package net.jxtz.jxtzos.spider.process;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.spider.SpiderLogServe;
import net.jxtz.jxtzos.entity.spider.SpiderNanchangHousingAuthority;
import net.jxtz.jxtzos.service.spider.SpiderNanchangHousingAuthorityService;
import net.jxtz.jxtzos.spider.BaseSpiderProcessor;
import net.jxtz.jxtzos.spider.SpiderDefinition;
import net.jxtz.jxtzos.spider.SpiderFactory;
import net.jxtz.jxtzos.util.ResponseUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 房管局
 * 商品房价格
 */

@Slf4j
@Component
public class NanchangHousingAuthorityProcessor implements BaseSpiderProcessor {

    @Autowired
    private SpiderNanchangHousingAuthorityService housingAuthorityService;

    private final AtomicInteger count = new AtomicInteger(1);

    private List<SpiderNanchangHousingAuthority> lists = new ArrayList<>();

    private Integer updateCount;

    private Integer lastTime;

    public Integer getLastTime() {
        return lastTime;
    }

    public Integer getUpdateCount() {
        return updateCount;
    }

    public final static String SPIDER_NAME = "nanchangHousingAuthority";

    private final static String FILE_PREFIX = "/Users/a123/Documents/test-housing-authority/";

    private final static int MAX_PAGE = 50;

    @Autowired
    private SpiderNanchangHousingAuthorityService service;

    public NanchangHousingAuthorityProcessor() {
        SpiderFactory instance = SpiderFactory.getInstance();
        instance.putSpiderProcess(SPIDER_NAME, this);
        SpiderDefinition nanchangHousingAuthority = new SpiderDefinition("南昌房管局公众号", SPIDER_NAME);
        instance.putSpiderDefinition(SPIDER_NAME, nanchangHousingAuthority);
    }

    // 拿到标题和PROJECTALL_ID
    private boolean process(Integer minPage) {

        String url = "https://appqy.ncfdc.com.cn/SxfHouseGS/SxfM/partList?sxf_name=keys&sxf_value=1CdF4bKo";
        List<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("pageIndex", minPage + ""));
        list.add(new BasicNameValuePair("hasNext", "true"));
        list.add(new BasicNameValuePair("isLoad", "true"));
        list.add(new BasicNameValuePair("newhtml", "false"));
        list.add(new BasicNameValuePair("mtype", "2"));
        String s = httpPostFormate(list, url);
        if (StringUtils.isBlank(s)) return false;
        Document parse = Jsoup.parse(s);
        Elements elementsByClass = parse.getElementsByClass("cell-block");
        for (Element byClass : elementsByClass) {
            log.info(byClass.toString());
            // 获取小区名
            Elements elementsByClass1 = byClass.getElementsByClass("cell-top");
            Element element = elementsByClass1.get(0);
            // 小区名
            String h3 = element.getElementsByTag("h3").get(0).ownText();
            log.info(h3);

            // 获取当期预售方案
            Elements elementsByClass2 = byClass.getElementsByClass("cell-left");
            Element element1 = elementsByClass2.get(0);
            // 楼栋和套数
            String h2 = element1.getElementsByTag("h3").get(0).text();
            // 时间范围
            String p = element1.getElementsByTag("p").get(0).text();

            log.info(h2 + " --> " + p);

            long time = System.currentTimeMillis() / 1000;

            // 获取图片
            Elements elementsByClass3 = byClass.getElementsByClass("cell-bottom");
            Element element2 = elementsByClass3.get(0);

            // 项目信息公示表
            String attr = element2.getElementsByTag("a").get(0).attr("imglist");
            log.info("项目信息公示表 --> " + attr);
            // 文件路径前缀 + 小区名 + 项目信息公示表 + 图片名称
            downloadPicture(attr, FILE_PREFIX + h3 + "/项目信息公示表/" + time);

            // 摇号方案
            String a = element2.getElementsByTag("a").get(2).attr("data-yaohaoimg");
            String[] split = a.split("\\|");
            log.info("摇号方案 --> ");
            for (String s1 : split) {
                downloadPicture(s1, FILE_PREFIX + h3 + "/摇号方案/" + time);
            }

            // 一房一价
            String a1 = element2.getElementsByTag("a").get(2).attr("data-priceimg");
            String[] split1 = a1.split("\\|");
            log.info("一房一价 --> ");
            for (String s1 : split1) {
                downloadPicture(s1, FILE_PREFIX + h3 + "/一房一价/" + time);
            }
        }

        return true;
    }


    private String httpPostFormate(List<NameValuePair> nameValuePairs, String url) {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Accept", "*/*");
        httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
        httpPost.setHeader("Connection", "keep-alive");
        httpPost.setHeader("Cache-Control", "no-cache");
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.setHeader("Cookie", "ASP.NET_SessionId=urgi2ulu5drrvahjqi3oy3xx");
        httpPost.setHeader("Host", "appqy.ncfdc.com.cn");
        httpPost.setHeader("Origin", "https://appqy.ncfdc.com.cn");
        httpPost.setHeader("Pragma", "no-cache");
        httpPost.setHeader("sec-ch-ua", "\"Google Chrome\";v=\"95\", \"Chromium\";v=\"95\", \";Not A Brand\";v=\"99\"");
        httpPost.setHeader("sec-ch-ua-mobile", "?0");
        httpPost.setHeader("sec-ch-ua-platform", "\"macOS\"");
        httpPost.setHeader("Sec-Fetch-Dest", "empty");
        httpPost.setHeader("Sec-Fetch-Site", "same-origin");
        httpPost.setHeader("Sec-Fetch-Mode", "cors");
        httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36");
        httpPost.setHeader("Referer", "https://appqy.ncfdc.com.cn/SxfHouseGS/SxfM/List?keys=1CdF4bKo");
        String entity = null;
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            entity = ResponseUtil.entityToString(response.getEntity());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return entity;
    }


    //链接url下载图片
    private static void downloadPicture(String urlList,String path) {
        URL url = null;
        File file = new File(path);
        try {
            FileUtils.forceMkdir(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(path + "/" + UUID.randomUUID().toString().replace("-", "") + ".jpg");
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            BASE64Encoder encoder = new BASE64Encoder();
            // 返回Base64编码过的字节数组字符串
            String encode = encoder.encode(buffer);
            log.info(encode);
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start(SpiderLogServe spiderLogServe, SpiderDefinition spiderDefinition) throws IOException {
        log.info("开始爬取");
        int minPage = 1;
        boolean flag = true;
        while (flag && minPage <= MAX_PAGE) {
            flag = process(minPage++);
        }
    }
}
